/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.vttu.ui;

import java.sql.Connection;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import vn.edu.vttu.data.Customer;
import vn.edu.vttu.data.VariableStatic;
import vn.edu.vttu.data.ConnectDB;

/**
 *
 * @author nhphuoc
 */
public class PanelTableReservation extends javax.swing.JPanel {

    /**
     * Creates new form PanelTableReservation
     */
    private int idTable;
    private int idCustomer;
    int index = 0;
    String x;
    JScrollPane scrollpane;
    private Connection conn = ConnectDB.conn();

    public PanelTableReservation() {
        initComponents();
        lbTableName.setText(VariableStatic.getNameTable());
        loadCustomer();
        getTime();
        if (tbCustomer.getRowCount() > 0) {
            VariableStatic.setIdCustomer(Integer.parseInt(String.valueOf(tbCustomer.getValueAt(0, 0))));
        }

    }

    private void loadCustomer() {
        tbCustomer.setModel(Customer.getLimit(conn));
        if (tbCustomer.getRowCount() > 0) {
            tbCustomer.setRowSelectionInterval(0, 0);
        }
        tbCustomer.getTableHeader().setReorderingAllowed(false);
    }

    private Timestamp getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime = formatter.format(dtTableReservation.getDate());
        Timestamp ts = Timestamp.valueOf(datetime);
        return ts;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lbTableName = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dtTableReservation = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        txtCustomerName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbCustomer = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        txtRepay = new javax.swing.JTextField();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Bàn Số");

        lbTableName.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbTableName.setForeground(new java.awt.Color(0, 0, 255));
        lbTableName.setText("Tên Bàn");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Trả Trước:");

        dtTableReservation.setDate(new Date());
        dtTableReservation.setDateFormatString("dd/MM/yyyy HH:mm");
        dtTableReservation.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtTableReservationPropertyChange(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Khách Hàng");

        txtCustomerName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCustomerNameKeyReleased(evt);
            }
        });

        tbCustomer = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        tbCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbCustomer.setRowHeight(20);
        tbCustomer.setSelectionBackground(new java.awt.Color(51, 204, 0));
        tbCustomer.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tbCustomer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbCustomerMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbCustomerMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbCustomer);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Ngày Nhận Bàn");

        txtRepay.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtRepayKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRepayKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbTableName, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtRepay, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dtTableReservation, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 86, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(lbTableName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dtTableReservation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtRepay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tbCustomerMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbCustomerMousePressed
        index = tbCustomer.getSelectedRow();
        VariableStatic.setIdCustomer(Integer.parseInt(String.valueOf(tbCustomer.getValueAt(index, 0))));
        txtCustomerName.setText((String) tbCustomer.getValueAt(index, 1));
    }//GEN-LAST:event_tbCustomerMousePressed

    private void txtCustomerNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCustomerNameKeyReleased
        try {
            tbCustomer.setModel(Customer.searchNamePhone(txtCustomerName.getText(), conn));
            tbCustomer.setRowSelectionInterval(0, 0);
            VariableStatic.setIdCustomer(Integer.parseInt(String.valueOf(tbCustomer.getValueAt(0, 0))));
        } catch (Exception e) {
            e.printStackTrace();
            tbCustomer.setModel(Customer.getLimit(conn));
            VariableStatic.setIdCustomer(Integer.parseInt(String.valueOf(tbCustomer.getValueAt(0, 0))));
            tbCustomer.getTableHeader().setReorderingAllowed(false);
        }
    }//GEN-LAST:event_txtCustomerNameKeyReleased

    private void tbCustomerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbCustomerMouseClicked

    }//GEN-LAST:event_tbCustomerMouseClicked

    private void dtTableReservationPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtTableReservationPropertyChange
        if (dtTableReservation.getDate().compareTo(new Date()) < 0) {
            dtTableReservation.setDate(new Date());
            VariableStatic.setDateTimeReservation(getTime());
        }
        VariableStatic.setDateTimeReservation(getTime());
    }//GEN-LAST:event_dtTableReservationPropertyChange

    private void txtRepayKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRepayKeyTyped
        int key = evt.getKeyChar();
        String st = txtRepay.getText();
        String stTest = "0123456789";
        if (key != evt.VK_BACK_SPACE
                && key != evt.VK_DELETE
                && key != evt.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
            }
            if (flag > 0) {

                evt.consume();
            }

        }
    }//GEN-LAST:event_txtRepayKeyTyped

    private void txtRepayKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRepayKeyReleased
        DecimalFormat df = new DecimalFormat("#,###,###");
        if (!txtRepay.getText().trim().equals("")) {
            Long num = Long.parseLong(txtRepay.getText().trim().replaceAll("\\.", ""));
            int prepay = Integer.parseInt(txtRepay.getText().trim().replaceAll("\\.", ""));
            txtRepay.setText(String.valueOf(df.format(num)));
            VariableStatic.setPrePay(prepay);
        }
    }//GEN-LAST:event_txtRepayKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser dtTableReservation;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTableName;
    private javax.swing.JTable tbCustomer;
    private javax.swing.JTextField txtCustomerName;
    private javax.swing.JTextField txtRepay;
    // End of variables declaration//GEN-END:variables
}
